package com.yanqu.road.dao.impl.monopolymarket.fortress;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.monopolymarket.fortress.MonopolyMarketFortressRewardData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/11/4 0:20
 */
public class MonopolyMarketFortressRewardDataDaoImpl  extends TempDataObjectDao<MonopolyMarketFortressRewardData> {

    public static String getMonopolyMarketFortressRewardDataSql = "select * from `t_c_monopolymarket_fortress_reward` where `idReceive`=false";
    public List<MonopolyMarketFortressRewardData> getMonopolyMarketFortressRewardDataList() {
        ResultSet rs = null;
        CallableStatement call = null;
        List<MonopolyMarketFortressRewardData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketFortressRewardDataSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateMonopolyMarketFortressRewardDataSql = "update `t_c_monopolymarket_fortress_reward` set unionName=?,rank=?,reward=?,idReceive=? where userId=? and weekId=? and fortressId=?";
    @Override
    protected boolean updateData(MonopolyMarketFortressRewardData monopolyMarketFortressRewardData) {
        monopolyMarketFortressRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressRewardData.getUnionName()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRewardData.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressRewardData.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketFortressRewardData.getIdReceive()));
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRewardData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRewardData.getFortressId()));
        if (getDb().executeNoneQuery(updateMonopolyMarketFortressRewardDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertMonopolyMarketFortressRewardDataSql = "insert into `t_c_monopolymarket_fortress_reward`(userId,weekId,unionName,fortressId,rank,reward,idReceive) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(MonopolyMarketFortressRewardData monopolyMarketFortressRewardData) {
        monopolyMarketFortressRewardData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, monopolyMarketFortressRewardData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRewardData.getWeekId()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressRewardData.getUnionName()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRewardData.getFortressId()));
        para.put(i++, new DbParameter(Types.INTEGER, monopolyMarketFortressRewardData.getRank()));
        para.put(i++, new DbParameter(Types.VARCHAR, monopolyMarketFortressRewardData.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, monopolyMarketFortressRewardData.getIdReceive()));
        if (getDb().executeNoneQuery(insertMonopolyMarketFortressRewardDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected MonopolyMarketFortressRewardData parseData(ResultSet rs) throws SQLException {
        MonopolyMarketFortressRewardData monopolyMarketFortressRewardData = new MonopolyMarketFortressRewardData();
        monopolyMarketFortressRewardData.setUserId(rs.getLong("userId"));
        monopolyMarketFortressRewardData.setWeekId(rs.getInt("weekId"));
        monopolyMarketFortressRewardData.setUnionName(rs.getString("unionName"));
        monopolyMarketFortressRewardData.setFortressId(rs.getInt("fortressId"));
        monopolyMarketFortressRewardData.setRank(rs.getInt("rank"));
        monopolyMarketFortressRewardData.setReward(rs.getString("reward"));
        monopolyMarketFortressRewardData.setIdReceive(rs.getBoolean("idReceive"));

        return monopolyMarketFortressRewardData;
    }

}
